package com.zx._03_并发.chapter_08_threadLocal;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolTest {

    // (2)
    final static ThreadLocal<LocalVariable> localVariable = new ThreadLocal<LocalVariable>();

    // (1)

    public static void main(String[] args) throws InterruptedException {
        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(5, 5, 100, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>());
        // (3)
        for (int i = 0; i < 50; ++i) {
            poolExecutor.execute(new Runnable() {
                @Override
                public void run() {
                    // (4)
                    localVariable.set(new LocalVariable());
                    // (5)
                    System.out.println("use local varaible");
                    //localVariable.remove();

                }
            });

            Thread.sleep(100);
        }
        // (6)
        System.out.println("pool execute over");

        Thread.sleep(1000);
        poolExecutor.shutdown();
        poolExecutor = null;
        System.gc();
    }

    static class LocalVariable {

        private Long[] a = new Long[1024 * 1024];
    }

}
